package com.ly.mp.dal.comm.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.support.lob.DefaultLobHandler;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.MybatisMapWrapperFactory;
import com.ly.mp.dal.comm.config.condition.TransactionPolicyNone;
import com.ly.mp.dal.comm.jdbc.DynamicDataSource;
import com.ly.mp.dal.comm.jdbc.PagedJdbcTemplate;

@Configuration
@Conditional({TransactionPolicyNone.class})
@MapperScan(basePackages = "com.ly.mp.**.mapper")
public class MpDataBasesConfiguration {

    @Bean(name = {"pagedJdbcTemplate"})
    @DependsOn({"dataSource"})
    public PagedJdbcTemplate pagedJdbcTemplate(DynamicDataSource dataSource, DataSourceInfo info) {
        PagedJdbcTemplate jdbcTemplate = new PagedJdbcTemplate(dataSource, info.getTransactionPolicy());
        jdbcTemplate.setLowerCaseColumn(!info.isUpperCaseColumn());
        return jdbcTemplate;
    }

    @Bean(name = {"lobHandler"})
    DefaultLobHandler defaultLobHandler() {
        return new DefaultLobHandler();
    }
    
    // 返回map集合下划线转驼峰  和 mapUnderscoreToCamelCase 一样
    @Bean
    public ConfigurationCustomizer mybatisConfigurationCustomizer(){
        return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
    }
}